package com.zw.mes.module.assemble.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.assemble.model.TestModel;
import com.zw.mes.module.assemble.service.TestingService;
import com.zw.mes.module.system.mapper.UserMapper;
import com.zw.mes.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 装配派工单 质检检验、历史检验
 * <p>
 * 权限：
 * @Author: China.wsg
 * @Date: 2021/04/24 16:12
 */
@Controller
@RequestMapping("/assemble/testing")
@SuppressWarnings("all")
public class TestingController extends BaseController {

    @Autowired
    private TestingService testingService;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/addPage")
    @RequiresPermissions({"asseOrd:test:view"})
    public String addPage(Model model) {
        model.addAttribute("user", userMapper.findByAccount(ShiroUtils.getUser().getAccount()));
        return "assemble/testing/testAdd";
    }

    /**
     * 查询待质检列表
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    @ResponseBody
    @PostMapping("/testList")
    @RequiresPermissions({"asseOrd:test:view"})
    public JsonResult testList(String barCode) {
        startPage();
        List<TestModel> list = testingService.findTestListByBarCode(barCode);
        return jsonResult(list);
    }

    /**
     * 质检合格
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/success")
    @RequiresPermissions({"asseOrd:test:success"})
    public JsonResult success(Integer id, Integer status, String result) {
        return jsonResult(testingService.success(id, status, result));
    }

    /**
     * 驳回/报废
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions(value = {"asseOrd:test:success", "asseOrd:test:check"}, logical = Logical.OR)
    public JsonResult reject(Integer id, Integer status, String reason) {
        return jsonResult(testingService.reject(id, status, reason));
    }

    @GetMapping("/selPage")
    @RequiresPermissions({"asseOrd:quality:view"})
    public String selPage() {
        return "assemble/testing/testSel";
    }

    /**
     * 派工单-历史检验列表
     *
     * @param applyModel
     * @return
     */
    @ResponseBody
    @PostMapping("/selList")
    @RequiresPermissions({"asseOrd:quality:view"})
    public JsonResult selList(TestModel testModel) {
        startPage();
        List<TestModel> list = testingService.findHistoryTestListByQuery(testModel);
        return jsonResult(list);
    }

    /**
     * 撤销质检检验
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"asseOrd:quality:success"})
    public JsonResult cancel(Integer id) {
        return jsonResult(testingService.cancelTest(id));
    }

}
